//
//  StatusFrame.m
//  SinaWebo
//
//  Created by 亦心 on 15/3/15.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "StatusFrame.h"
#import "NSAttributedString+Extension.h"
#import "Status.h"
#import "User.h"
#import "EmotionTool.h"
#import "PhotosView.h"

@implementation StatusFrame

-(void)setStatus:(Status *)status{
    _status=status;

    //cell的高度
    CGFloat cellW=kScreenW-kCellMargin*2;
    //TopView
    CGFloat topViewX=0;
    CGFloat topViewY=0;
    CGFloat topViewW=cellW;
    CGFloat topViewH=0;
    
    //头像
    CGFloat iconViewX=StatusFrameMargin;
    CGFloat iconViewY=StatusHeaderTop;
    CGFloat iconViewWH=kPeopleWH;
    _iconViewF=CGRectMake(iconViewX, iconViewY, iconViewWH, iconViewWH);
    
    //认证图标
    CGFloat verifiedW = kVerified;
    CGFloat verifiedH = verifiedW;
    CGFloat verifiedX = CGRectGetMaxX(_iconViewF) - verifiedW;
    CGFloat verifiedY = CGRectGetMaxY(_iconViewF) - verifiedH;
    _verifiedF = CGRectMake(verifiedX, verifiedY, verifiedW, verifiedH);
    
     //昵称
    CGFloat nameLabelX=CGRectGetMaxX(_iconViewF)+StatusFrameMargin;
    CGFloat nameLabelY=iconViewY + StatusTimeTop;
    CGSize nameLabelSize=[status.user.name sizeWithFont:StatusFrameNameLabelFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _nameLabelF=(CGRect){{nameLabelX,nameLabelY},nameLabelSize};
    
    //会员图标
    if (status.user.mbtype) {
        CGFloat vipViewX=CGRectGetMaxX(_nameLabelF)+StatusFrameMargin;
        CGFloat vipViewY=nameLabelY;
        CGFloat vipViewW=kVip;
        CGFloat vipViewH=nameLabelSize.height;
        _vipViewF=CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH);
    }
    
    //时间
    CGFloat timeLabelX= nameLabelX;
    CGFloat timeLabelY= CGRectGetMaxY(_nameLabelF) + StatusTimeTop;
    CGSize timeLabelSize=[status.created_at2 sizeWithFont:StatusFrameTimeLabelFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _timeLabelF=(CGRect){{timeLabelX,timeLabelY},timeLabelSize};
    
    //来源
    CGFloat sourceLabelX=CGRectGetMaxX(_timeLabelF)+StatusFrameMargin;
    CGFloat sourceLabelY=timeLabelY;
    CGSize sourceLabelSize=[status.source sizeWithFont:StatusFrameTimeLabelFont];
    _sourceLabelF=(CGRect){{sourceLabelX,sourceLabelY},sourceLabelSize};
    
    //正文
    CGFloat contentLabelX=iconViewX;
    CGFloat contentLabelY=MAX(CGRectGetMaxY(_timeLabelF), CGRectGetMaxY(_iconViewF)) + StatusFrameMargin;
    CGFloat contentMaxW=topViewW-2*StatusFrameMargin;
   _textAttr = [[EmotionTool sharedEmotion] attributedString:status.text font:StatusFrameTextLabelFont];
    CGFloat contentLabelH = [_textAttr attributedSize:CGSizeMake(contentMaxW, MAXFLOAT)].height;
    _contentLabelF= CGRectMake(contentLabelX, contentLabelY, contentMaxW, contentLabelH);
    
    //配图
    if (status.pic_urls.count) {
        CGSize photoViewSize=[PhotosView photosViewSizeWithDataArrray:status.pic_urls];
        CGFloat photoViewX=contentLabelX;
        CGFloat photoViewY=CGRectGetMaxY(_contentLabelF)+StatusFrameMargin;
        _photoViewF=(CGRect){{photoViewX,photoViewY},photoViewSize};
    }
    
    
    //被转发微博
    if (status.retweeted_status) {
        CGFloat retweetViewX=contentLabelX;
        CGFloat retweetViewY=CGRectGetMaxY(_contentLabelF)+StatusFrameMargin*0.5;
        CGFloat retweetViewW=kScreenW;
        CGFloat retweetViewH=0;
        
        //被转发微博的正文\内容
        CGFloat retweetContentLabelX=retweetViewX;
        CGFloat retweetContentLabelY=StatusFrameMargin;
        CGFloat retweetContentLabelMaxW=cellW - 2 * StatusFrameMargin;
        NSString *retText = [NSString stringWithFormat:@"@%@:%@",status.retweeted_status.user.name, status.retweeted_status.text];
        _retweetedText = [[EmotionTool sharedEmotion] attributedString:retText font:StatusFrameRewTextLabelFont];
        CGFloat retweetContentH = [_retweetedText attributedSize:CGSizeMake(retweetContentLabelMaxW, MAXFLOAT)].height;
        _retweetContentLabelF= CGRectMake(retweetContentLabelX,retweetContentLabelY, retweetContentLabelMaxW, retweetContentH);

        //被转发微博的配图
        if (status.retweeted_status.pic_urls.count) {
            CGFloat retweetPhotoViewX=retweetContentLabelX;
            CGFloat retweetPhotoViewY=CGRectGetMaxY(_retweetContentLabelF)+StatusFrameMargin;
            CGSize retweetPhotoView = [PhotosView photosViewSizeWithDataArrray:status.retweeted_status.pic_urls];
            _retweetPhotoViewF=(CGRect){{retweetPhotoViewX,retweetPhotoViewY},retweetPhotoView};
            
            //有配图时被转发微博的高度
            retweetViewH=CGRectGetMaxY(_retweetPhotoViewF)+StatusFrameMargin;
        }else{
            //没有配图时被转发微博的高度
            retweetViewH=CGRectGetMaxY(_retweetContentLabelF)+StatusFrameMargin;
        }
        _retweetViewF=CGRectMake(0, retweetViewY, retweetViewW, retweetViewH);
        //有转发微博时微博的高度
        topViewH=CGRectGetMaxY(_retweetViewF);
    }else{
        //没有转发微博时微博的高度
        //有配图时微博的高度
        if (status.thumbnail_pic) {
            topViewH=CGRectGetMaxY(_photoViewF)+StatusFrameMargin;
        }else{
            //没有配图时微博的高度
            topViewH=CGRectGetMaxY(_contentLabelF)+StatusFrameMargin;
        }
    }
    _topViewF=CGRectMake(topViewX, topViewY, topViewW, topViewH);
    
    //工具条
    CGFloat statusToolbarX=topViewX;
    CGFloat statusToolbarY=CGRectGetMaxY(_topViewF);
    CGFloat statusToolbarW=topViewW;
    CGFloat statusToolbarH=36;
    _statusToolbarF=CGRectMake(statusToolbarX, statusToolbarY, statusToolbarW, statusToolbarH);
    
    //cell的高度
    _cellHeight=CGRectGetMaxY(_statusToolbarF)+10;
}


@end
